#include "stdafx.h"

void LinearBulletStrategy::idea(double & x, double & y, double dtime, double time, double & vx, double & vy)
{
	x += vx*dtime;
	y += vy*dtime;
	//return { (int)(curPos.X + dtime*vx),(int)(curPos.Y + dtime*vy),vx,vy };
}

void TrackBulletStrategy::idea(double & x, double & y, double dtime, double time, double & vx, double & vy)
{
	//time += dtime;

	if (time <= lasting)
	{
		Point heroPos = Game::getInstance()->getHero()->getPoint();
		double angle = atan2(heroPos.Y - y, x - heroPos.X);

		double v = sqrt(vx*vx + vy*vy);

		vx = -v*cos(angle);
		vy = v*sin(angle);

		//int nvx = -v*cos(angle);
		//int nvy = v*sin(angle);

		//return { (int)(curPos.X - dtime*v*cos(angle)),(int)(curPos.Y + dtime*v*sin(angle)),nvx,nvy };
		x += vx*dtime;
		y += vy*dtime;
	}
	else
	{
		//return { (int)(curPos.X + dtime*vx),(int)(curPos.Y + dtime*vy),vx,vy };
		x += vx*dtime;
		y += vy*dtime;
	}
}

void EnemyStrategyLinear::idea(double & x, double & y, double dtime, double time, double & vx, double & vy, int & dir, int width, int height)
{
	switch (dir)
	{
	case DIR_UP:
		y -= dtime*vx;
		break;
	case DIR_DOWN:
		y += dtime*vx;
		break;
	case DIR_LEFT:
		x -= dtime*vx;
		break;
	case DIR_RIGHT:
		x += dtime*vx;
		break;
	}

	Rect rc = { (int)(x - width / 2),(int)(y - height / 2),width,height };
	if (Map::getInstance()->collide(rc) || Game::getInstance()->getHero()->collide(rc))
	{
		switch (dir)
		{
		case DIR_UP:
			y += dtime*vx;
			break;
		case DIR_DOWN:
			y -= dtime*vx;
			break;
		case DIR_LEFT:
			x += dtime*vx;
			break;
		case DIR_RIGHT:
			x -= dtime*vx;
			break;
		}
		dir = rand() % 4 + 1;
	}
}

void EnemyStrategyStatic::idea(double & x, double & y, double dtime, double time, double & vx, double & vy, int & dir, int width, int height)
{
	//do nothing
}

bool GameKillAll::win()
{
	if (EnemyMgr::getNum() <= 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool GameKillAll::lose()
{
	if (Game::getInstance()->getHero()->getLife() <= 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool GameEndPoint::win()
{
	return Game::getInstance()->endpoint->collide(Game::getInstance()->hero->getRect());
}

bool GameEndPoint::lose()
{
	return false;
}

bool GameCollection::win()
{
	if (CollectionMgr::getCount() == 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool GameCollection::lose()
{
	return false;
}